'use strict'

myApp
.controller("roleAddCtrl", ["$scope", "$state", "$http", function($scope, $state, $http){
	$scope.save = function(role){
		$http({
			method: "post",
			url: "http://127.0.0.1:8008/roleAdd",
			headers:{
				'Content-Type': 'application/x-www-form-urlencoded'
			},
			data: role
		}).success(function(data){
			if(data == "success"){
				alert("添加成功！")
				$state.go("/admin.role_list");
			}else if(data == "error"){
				alert("添加失败！")
				$state.reload();
			}
		});
	}
	$scope.back = function(){
		$state.go("/admin.role_list");
	}
}])
.controller("roleListCtrl", ["$scope", "$state", "$http", function($scope, $state, $http){
	$http({
		method: "get",
		url: "http://127.0.0.1:8008/roleList",
	}).success(function(data){
		if(data == "none"){
			alert("未登陆系统，请登陆系统！");
			$state.go("/login");
		}else{
			$scope.roles = data;
		}
	});
	$scope.del = function(role, $event){
		$event.stopPropagation();
		$http({
			method: "post",
			url: "http://127.0.0.1:8008/roleDel",
			headers:{
				'Content-Type': 'application/x-www-form-urlencoded'
			},
			data: role
		}).success(function(data){
			if(data){
				alert("删除成功！")
				$state.reload();
			}
		});
	};
}])
.controller("roleSmCtrl", ["$scope", "$state", "$http", "$stateParams", function($scope, $state, $http, $stateParams){
	$http({
		method: "get",
		url: "http://127.0.0.1:8008/roleModify",
		params: {
			"id": $stateParams.id
		}
	}).success(function(data){
		$scope.role = data;
	});

	$scope.save = function(role){
		$http({
			method: "post",
			url: "http://127.0.0.1:8008/roleModify",
			headers:{
				'Content-Type': 'application/x-www-form-urlencoded'
			},
			data: role
		}).success(function(data){
			if(data){
				alert("修改成功")
				$state.go("/admin.role_list");
			}
		});
	}
	$scope.back = function(){
		$state.go("/admin.role_list");
	}
}])
.controller("groupAddCtrl", ["$scope", "$state", "$http", function($scope, $state, $http){
	$http({
		method: "get",
		url: "http://127.0.0.1:8008/userList",
		headers: {
			'Content-Type': 'application/x-www-form-urlencoded'
		}
	}).success(function(data){
		$scope.users = data;
	});


	$scope.save = function(group){
		console.log(group);
		// $http({
		// 	method: "post",
		// 	url: "http://127.0.0.1:8008/groupAdd",
		// 	headers:{
		// 		'Content-Type': 'application/x-www-form-urlencoded'
		// 	},
		// 	data: group
		// }).success(function(data){
		// 	if(data == "success"){
		// 		alert("添加成功！");
		// 		$state.go("/admin.group_list")
		// 	}else{
		// 		$state.go("/login")
		// 	}
		// });
	}
	$scope.back = function(){
		$state.go("/admin.group_list");
	}
}])
.controller("groupListCtrl", ["$scope", "$state", "$http", function($scope, $state, $http){
	$http({
		method: "get",
		url: "http://127.0.0.1:8008/groupList"
	}).success(function(data){
		$scope.groups = data;
	});
	$scope.del = function(group){
		$http({
			method: "post",
			url: "http://127.0.0.1:8008/groupDel",
			headers:{
				'Content-Type': 'application/x-www-form-urlencoded'
			},
			data: group
		}).success(function(data){
			if(data){
				alert("删除成功！")
				$state.reload();
			}
		});
	};
}])
.controller("groupSmCtrl", ["$scope", "$state", "$http", "$stateParams", function($scope, $state, $http, $stateParams){
	$http({
		method: "get",
		url: "http://127.0.0.1:8008/groupModify",
		params: {
			id: $stateParams.id
		}
	}).success(function(data){
		$scope.group = data;
	});

	$scope.save = function(group){
		$http({
			method: "post",
			url: "http://127.0.0.1:8008/groupModify",
			headers:{
					'Content-Type': 'application/x-www-form-urlencoded'
				},
			data: group
		}).success(function(data){
			alert("修改成功！");
			$state.go("/admin.group_list")
		});
	}
	$scope.back = function(){
		$state.go("/admin.group_list");
	}
}])
.controller("privilegeAddCtrl", ["$scope", "$state", "$http", function($scope, $state, $http){
	$scope.save = function(privilege){
		$http({
			method: "post",
			url: "http://127.0.0.1:8008/privilegeAdd",
			headers:{
				'Content-Type': 'application/x-www-form-urlencoded'
			},
			data: privilege
		}).success(function(data){
			if(data == "success"){
				alert("添加成功！");
				$state.go("/admin.privilege_list")
			}else{
				$state.go("/login")
			}
		});
	}
}])
.controller("privilegeListCtrl", ["$scope", "$state", "$http", function($scope, $state, $http){
	$http({
		method: "get",
		url: "http://127.0.0.1:8008/privilegeList"
	}).success(function(data){
		$scope.privileges = data;
	});
	$scope.del = function(privilege){
		$http({
			method: "post",
			url: "http://127.0.0.1:8008/privilegeDel",
			headers:{
				'Content-Type': 'application/x-www-form-urlencoded'
			},
			data: privilege
		}).success(function(data){
			if(data){
				alert("删除成功！")
				$state.reload();
			}
		});
	};
}])
.controller("privilegeSmCtrl", ["$scope", "$state", "$http", "$stateParams", function($scope, $state, $http, $stateParams){
	$http({
		method: "get",
		url: "http://127.0.0.1:8008/privilegeModify",
		params: {
			id: $stateParams.id
		}
	}).success(function(data){
		$scope.privilege = data;
	});

	$scope.save = function(privilege){
		$http({
			method: "post",
			url: "http://127.0.0.1:8008/privilegeModify",
			headers:{
				'Content-Type': 'application/x-www-form-urlencoded'
			},
			data: privilege
		}).success(function(data){
			alert("修改成功！");
			$state.go("/admin.privilege_list")
		});
	}
}])
.controller("userListCtrl", ["$scope", "$state", "$http", function($scope, $state, $http){
	$http({
		method: "get",
		url: "http://127.0.0.1:8008/userList"
	}).success(function(data){
		$scope.users = data;
	});
	$scope.del = function(user){
		$http({
			method: "post",
			url: "http://127.0.0.1:8008/userDel",
			headers:{
				'Content-Type': 'application/x-www-form-urlencoded'
			},
			data: user
		}).success(function(data){
			if(data){
				alert("删除成功！")
				$state.reload();
			}
		});
	};
}])
.controller("userAddCtrl", ["$scope", "$state", "$http", function($scope, $state, $http){
	$scope.save = function(user){
		$http({
			method: "post",
			url: "http://127.0.0.1:8008/userAdd",
			headers:{
				'Content-Type': 'application/x-www-form-urlencoded'
			},
			data: user
		}).success(function(data){
			if(data == "success"){
				alert("添加成功！")
				$state.go("/admin.user_list");
			}else if(data == "error"){
				alert("添加失败！")
				$state.reload();
			}
		});
	}
	$scope.back = function(){
		$state.go("/admin.user_list");
	}
}])
.controller("userSmCtrl", ["$scope", "$state", "$http", "$stateParams", function($scope, $state, $http, $stateParams){
	$http({
		method: "get",
		url: "http://127.0.0.1:8008/userModify",
		params: {
			"id": $stateParams.id
		}
	}).success(function(data){
		$scope.user = data;
	});

	$scope.save = function(user){
		$http({
			method: "post",
			url: "http://127.0.0.1:8008/userModify",
			headers:{
				'Content-Type': 'application/x-www-form-urlencoded'
			},
			data: user
		}).success(function(data){
			if(data){
				alert("修改成功")
				$state.go("/admin.user_list");
			}
		});
	}
	$scope.back = function(){
		$state.go("/admin.user_list");
	}
}]);








